package ${package.ServiceImpl};

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${package.Entity}.${entity};
import ${cfg.enumPackage}.BaseMessageEnum;
import ${cfg.requestPackage}.${cfg.moduleName}.${entity}CreateRequest;
import ${cfg.requestPackage}.${cfg.moduleName}.${entity}PageRequest;
import ${cfg.requestPackage}.${cfg.moduleName}.${entity}UpdateRequest;
import ${cfg.responsePackage}.${cfg.moduleName}.${entity}DetailResponse;
import ${cfg.responsePackage}.${cfg.moduleName}.${entity}PageResponse;
import ${package.Mapper}.${table.mapperName};
import ${cfg.repositoryPackage}.${table.serviceName?replace("Service", "Repository")};
import ${package.Service}.${table.serviceName};
import com.laicui.wms.core.BaseException;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * ${table.comment!} 服务实现类
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Service
@AllArgsConstructor
public class ${table.serviceImplName} implements ${table.serviceName} {

    private final ${table.serviceName?replace("Service", "Repository")} ${entity?uncap_first}Repository;

    @Override
    public ${entity}DetailResponse find${entity}Detail(String id) {
        ${entity} ${entity?uncap_first} = ${entity?uncap_first}Repository.getById(id);
        if (${entity?uncap_first} == null) {
            throw new BaseException(BaseMessageEnum.NOT_FOUND_OBJECT, "${table.comment!}不存在");
        }
        ${entity}DetailResponse response = new ${entity}DetailResponse();
        BeanUtils.copyProperties(${entity?uncap_first}, response);
        return response;
    }

    @Override
    public Page<${entity}PageResponse> find${entity}Page(${entity}PageRequest request) {
        Page<${entity}PageResponse> pageResponse = new Page<>();
        
        Page<${entity}> ${entity?uncap_first}Page = ${entity?uncap_first}Repository.select${entity}Page(request);
        if (${entity?uncap_first}Page.getRecords().isEmpty()) {
            BeanUtils.copyProperties(${entity?uncap_first}Page, pageResponse);
            return pageResponse;
        }
        
        pageResponse = (Page<${entity}PageResponse>) ${entity?uncap_first}Page.convert(${entity?uncap_first} -> {
            ${entity}PageResponse response = new ${entity}PageResponse();
            BeanUtils.copyProperties(${entity?uncap_first}, response);
            return response;
        });
        
        return pageResponse;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create${entity}(${entity}CreateRequest request) {
        ${entity} add = new ${entity}();
        BeanUtils.copyProperties(request, add);
        ${entity?uncap_first}Repository.save(add);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update${entity}(${entity}UpdateRequest request) {
        ${entity} exists = ${entity?uncap_first}Repository.getById(request.getId());
        if (exists == null) {
            throw new BaseException(BaseMessageEnum.NOT_FOUND_OBJECT, "${table.comment!}不存在");
        }
        
        ${entity} update = new ${entity}();
        BeanUtils.copyProperties(request, update);
        ${entity?uncap_first}Repository.updateById(update);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete${entity}(String id) {
        ${entity} exists = ${entity?uncap_first}Repository.getById(id);
        if (exists == null) {
            throw new BaseException(BaseMessageEnum.NOT_FOUND_OBJECT, "${table.comment!}不存在");
        }
        ${entity?uncap_first}Repository.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete${entity}(List<String> ids) {
        ${entity?uncap_first}Repository.removeByIds(ids);
    }
}